<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java管道流：线程间通信的艺术</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-container {
            background: linear-gradient(135deg, #1e1e2e 0%, #2d2d44 100%);
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-header {
            background: rgba(255,255,255,0.05);
            padding: 12px 20px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
            color: #e6e6e6;
        }
        .keyword { color: #c678dd; }
        .string { color: #98c379; }
        .comment { color: #5c6370; font-style: italic; }
        .class-name { color: #e06c75; }
        .function { color: #61afef; }
        .number { color: #d19a66; }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.15);
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                    Java管道流
                </h1>
                <p class="text-2xl md:text-3xl mb-4 opacity-90">
                    线程间通信的优雅艺术
                </p>
                <p class="text-lg md:text-xl opacity-80 max-w-3xl mx-auto">
                    探索PipedOutputStream与PipedInputStream的协作之美，掌握Java多线程编程中的精妙通信机制
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-lg p-8 mb-12 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 serif-font">
                <i class="fas fa-stream mr-3 text-purple-600"></i>
                什么是管道流？
            </h2>
            <p class="text-lg text-gray-700 leading-relaxed">
                <span class="drop-cap serif-font">管</span>道流是Java中实现线程间通信的重要机制，它提供了一种优雅的方式让两个线程之间进行数据传输。管道流主要包含两个核心组件：<span class="font-semibold text-purple-700">管道输出流（PipedOutputStream）</span>和<span class="font-semibold text-purple-700">管道输入流（PipedInputStream）</span>。通过将输出流连接到输入流上，我们可以实现线程间的数据传递。
            </p>
        </div>

        <!-- Connection Method -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-link mr-2 text-purple-600"></i>
                    连接方法
                </h3>
                <div class="bg-gray-100 rounded-lg p-4 font-mono text-sm">
                    <span class="text-purple-600">public void</span> <span class="text-blue-600">connect</span>(<span class="text-green-600">PipedInputStream</span> snk) <span class="text-purple-600">throws</span> <span class="text-red-600">IOException</span>
                </div>
                <p class="mt-4 text-gray-700">
                    使用PipedOutputStream类提供的connect()方法，可以将输出流与输入流建立连接，形成一个完整的管道通信通道。
                </p>
            </div>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-cogs mr-2 text-purple-600"></i>
                    工作原理
                </h3>
                <ul class="space-y-3 text-gray-700">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span>一个线程通过PipedOutputStream写入数据</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span>另一个线程通过PipedInputStream读取数据</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span>数据在管道中自动传输，无需额外同步</span>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Visual Diagram -->
        <div class="bg-white rounded-2xl shadow-lg p-8 mb-12">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 text-center">
                <i class="fas fa-project-diagram mr-2 text-purple-600"></i>
                管道流通信架构
            </h3>
            <div class="mermaid">
                graph LR
                    A[发送线程] -->|写入数据| B[PipedOutputStream]
                    B -->|connect()| C[管道连接]
                    C -->|数据流动| D[PipedInputStream]
                    D -->|读取数据| E[接收线程]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#e0e7ff,stroke:#667eea,stroke-width:2px
                    style D fill:#e0e7ff,stroke:#764ba2,stroke-width:2px
                    style C fill:#fbbf24,stroke:#f59e0b,stroke-width:2px
            </div>
        </div>

        <!-- Code Example -->
        <div class="mb-12">
            <h3 class="text-3xl font-bold mb-6 text-gray-800 serif-font">
                <i class="fas fa-code mr-3 text-purple-600"></i>
                完整示例代码
            </h3>
            
            <div class="code-container">
                <div class="code-header">
                    <div class="flex items-center justify-between">
                        <div class="flex items-center space-x-2">
                            <div class="w-3 h-3 bg-red-500 rounded-full"></div>
                            <div class="w-3 h-3 bg-yellow-500 rounded-full"></div>
                            <div class="w-3 h-3 bg-green-500 rounded-full"></div>
                        </div>
                        <span class="text-gray-400 text-sm">Test01.java</span>
                    </div>
                </div>
                <pre><code><span class="keyword">public class</span> <span class="class-name">Test01</span> {
    <span class="keyword">public static void</span> <span class="function">main</span>(<span class="class-name">String</span>[] args) <span class="keyword">throws</span> <span class="class-name">Exception</span> {
        
        <span class="class-name">Send</span> send = <span class="keyword">new</span> <span class="class-name">Send</span>();
        <span class="class-name">Receive</span> receive = <span class="keyword">new</span> <span class="class-name">Receive</span>();
        
        <span class="comment">// 连接管道 - 这是关键步骤！</span>
        send.<span class="function">getPos</span>().<span class="function">connect</span>(receive.<span class="function">getPis</span>());
        
        <span class="comment">// 启动两个线程</span>
        <span class="keyword">new</span> <span class="class-name">Thread</span>(send).<span class="function">start</span>();
        <span class="keyword">new</span> <span class="class-name">Thread</span>(receive).<span class="function">start</span>();
    }
}

<span class="comment">// 发送线程类</span>
<span class="keyword">class</span> <span class="class-name">Send</span> <span class="keyword">implements</span> <span class="class-name">Runnable</span> {
    
    <span class="keyword">private</span> <span class="class-name">PipedOutputStream</span> pos = <span class="keyword">null</span>; <span class="comment">// 定义管道输出流</span>
    
    <span class="keyword">public</span> <span class="function">Send</span>() {
        <span class="keyword">this</span>.pos = <span class="keyword">new</span> <span class="class-name">PipedOutputStream</span>();
    }
    
    <span class="keyword">@Override</span>
    <span class="keyword">public void</span> <span class="function">run</span>() {
        <span class="class-name">String</span> str = <span class="string">"Hello World"</span>; <span class="comment">// 定义输出信息</span>
        <span class="keyword">try</span> {
            pos.<span class="function">write</span>(str.<span class="function">getBytes</span>()); <span class="comment">// 输出字节数组</span>
        } <span class="keyword">catch</span> (<span class="class-name">IOException</span> e) {
            e.<span class="function">printStackTrace</span>();
        } <span class="keyword">finally</span> {
            <span class="keyword">try</span> {
                pos.<span class="function">close</span>(); <span class="comment">// 关闭流</span>
            } <span class="keyword">catch</span> (<span class="class-name">IOException</span> e) {
                e.<span class="function">printStackTrace</span>();
            }
        }
    }
    
    <span class="keyword">public</span> <span class="class-name">PipedOutputStream</span> <span class="function">getPos</span>() { <span class="comment">// 获取输出流</span>
        <span class="keyword">return</span> pos;
    }
}

<span class="comment">// 接收线程类</span>
<span class="keyword">class</span> <span class="class-name">Receive</span> <span class="keyword">implements</span> <span class="class-name">Runnable</span> {
    
    <span class="keyword">private</span> <span class="class-name">PipedInputStream</span> pis = <span class="keyword">null</span>;
    
    <span class="keyword">public</span> <span class="function">Receive</span>() {
        pis = <span class="keyword">new</span> <span class="class-name">PipedInputStream</span>(); <span class="comment">// 实例化输入流</span>
    }
    
    <span class="keyword">@Override</span>
    <span class="keyword">public void</span> <span class="function">run</span>() {
        <span class="keyword">byte</span>[] by = <span class="keyword">new byte</span>[<span class="number">1024</span>];
        <span class="keyword">int</span> read = <span class="number">0</span>;
        <span class="keyword">try</span> {
            read = pis.<span class="function">read</span>(by); <span class="comment">// 读取到数组中</span>
        } <span class="keyword">catch</span> (<span class="class-name">IOException</span> e) {
            e.<span class="function">printStackTrace</span>();
        } <span class="keyword">finally</span> {
            <span class="keyword">try</span> {
                pis.<span class="function">close</span>();
            } <span class="keyword">catch</span> (<span class="class-name">IOException</span> e)